﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Mvc;
using Business;
using Common;
using Dto;

namespace Manage.Controllers
{
    public class ChannelController : BaseController
    {
        private readonly string ChannelOriginalPath = ConfigurationManager.AppSettings["ChannelOriginalPath"];
        private readonly AnnexBusiness annexBusiness = new AnnexBusiness();
        private readonly ChannelBusiness channelBusiness = new ChannelBusiness();
        private string ChannelNarrowPath = ConfigurationManager.AppSettings["ChannelNarrowPath"];

        public ActionResult List(int? currentPage)
        {
            int totalCount;
            List<SiteChannelDto> dtos = channelBusiness.GetList(currentPage ?? 1, 10, out totalCount);
            var page = new PagingDto(totalCount, currentPage ?? 1, 10, dtos ?? new List<SiteChannelDto>());
            return View(page);
        }

        public ActionResult Edit(int? id)
        {
            SiteChannelDto dto = channelBusiness.GetSiteChannelById(id ?? 0) ?? new SiteChannelDto();
            ViewData["ChannelImgs"] = channelBusiness.GetIndexImgs(dto.ChannelIndexImg);
            return View(dto);
        }

        public ActionResult SaveChannel(string name, int templateId, string code, int id, int state, string imgIds,
                                        HttpPostedFileBase fileImg)
        {
            var annexUploadUtil = new AnnexUploadUtil();
            AnnexDto annexDto = annexUploadUtil.ImageUpload(ChannelOriginalPath, fileImg, null, null);
            var dto = new SiteChannelDto
                          {
                              ChannelCode = code,
                              ChannelCurState = state,
                              ChannelId = id,
                              ChannelIndexImg = imgIds,
                              ChannelName = name,
                              ChannelTemplateId = templateId,
                              ChannelTime = DateTime.Now
                          };
            bool b = channelBusiness.Save(dto, annexDto);
            if (b)
            {
                Response.Write("<script>alert('保存频道信息成功！');location.href='" + Url.Action("List") + "';</script>");
                Response.End();
            }
            return RedirectToAction("List");
        }
    }
}